#include "mat_sfx_collection.h"
#include "mat_sfx.h"

#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros

MATSFXCollection::MATSFXCollection(MATSfx::SFX_TYPES Type) : m_Type(Type)
{

}



void		MATSFXCollection::Render(const math::Vec2& Offset,float Scale)
{
	MATSfx*  pSFX;
	for(unsigned int i = 0; i < m_SFXs.size(); i++)
	{
		pSFX = m_SFXs[i];
		if(!pSFX->GetIsPostRender())
			pSFX->Render(Offset,Scale);		

	}
}

void		MATSFXCollection::PostRender(const math::Vec2& Offset,float Scale)
{
	MATSfx*  pSFX;
	for(unsigned int i = 0; i < m_SFXs.size(); i++)
	{
		pSFX = m_SFXs[i];
		if(pSFX->GetIsPostRender())
			pSFX->Render(Offset,Scale);		

	}
}



void		MATSFXCollection::RenderDebug(const math::Vec2& Offset,float Scale)
{
	MATSfx*  pSFx;
	for(unsigned int i = 0; i < m_SFXs.size(); i++)
	{
		pSFx = m_SFXs[i];
		pSFx->RenderDebug(Offset,Scale);
	}
}


void		MATSFXCollection::Tick(float dt)
{
	for(unsigned int i = 0; i < m_SFXs.size(); i++)
	{
		MATSfx*  pSFX = m_SFXs[i];
		pSFX->Tick(dt);
	}
}


void		MATSFXCollection::Clear()
{
	for(unsigned int i = 0; i < m_SFXs.size(); i++)
	{
		MATSfx*  pSFX = m_SFXs[i];
		pSFX->Clear();
	}

	
	for( int i = (int)m_SFXs.size() - 1; i >= 0; i--)
	{
		MATSfx*  pSFX = m_SFXs[i];
		UTIL_SAFE_DELETE(pSFX);
	}
	m_SFXs.resize(0);
}
	
void		MATSFXCollection::ForceFinish()
{
	for( int i = (int)m_SFXs.size() - 1; i >= 0; i--)
	{
		MATSfx*  pSFX = m_SFXs[i];
		pSFX->ToDie();
	}
}

MATSfx*		MATSFXCollection::Spawn(	const math::Vec2& Pos, 											
											float SizeScale,
											float Rotation,
											MATSounds::SOUND_NAMES SoundSFXName 
										  )
{
	for( int i = 0; i < (int)m_SFXs.size(); i++)
	{
		MATSfx*  pSFX = m_SFXs[i];
		if(!pSFX->IsAlive())
		{
			pSFX->ReSpawn(	Pos,
							SizeScale,
							Rotation,
							SoundSFXName
							);
			return pSFX;
		}
	}

	MATSfx*  pSFX = new MATSfx(m_Type);
	m_SFXs.push_back(pSFX);
	
	pSFX->ReSpawn(	Pos,
					SizeScale,
					Rotation,
					SoundSFXName
					);
	return pSFX;
}

	